<!--  -->
<template>
  <div>
    <item-title title="稳定性试验放样信息" :borderShow="false"/>

    <div class="box_table" v-for="(val,key,idx) in dataTable" :key="idx">
      <div class="title">{{val.name}}的稳定性试验放样信息</div>
      <el-table :data="val.list" border style="width: 100%">
        <el-table-column label="名称" align="center">
          <template #default="{row}">
            {{row.sample.sample_name}}
          </template>
        </el-table-column>

        <el-table-column label="批号" align="center">
          <template #default="{row}">
            {{ handlSample(row.sample.batch_info)}}
          </template>
        </el-table-column>

        <el-table-column prop="value" label="单倍量" align="center">
          <template #default="{row}">
            {{row.value }}{{row.sample.packaging_unit}}
          </template>
        </el-table-column>

        <el-table-column label="2倍量" align="center">
          <template #default="{row}">
            {{row.value * 2}}{{row.sample.packaging_unit}}
          </template>
        </el-table-column>

        <el-table-column label="3倍量" align="center">
          <template #default="{row}">
            {{row.value * 3}}{{row.sample.packaging_unit}}
          </template>
        </el-table-column>

      </el-table>

    </div>

    <div class="box_table">
      <div class="title">样品统计</div>
      <el-table :data="experiment" border="" class="table1" style="width: 100%">
        <el-table-column v-for="(cel,index) in headData" :key="index+ 'ss'" :label="cel.label.label"
                         :prop="cel.prop" align="center" width="200">
          <template v-if="cel.children.length > 0">
            <el-table-column v-for="(col,ind) in cel.children" :key="ind +'xx'" :label="col.label"
                             align="center">
              <template slot-scope="scope">
                <template v-if="col.label === '单倍量'">
                  {{scope.row[col.prop] === 0 ? '/':scope.row[col.prop]}}
                  {{scope.row[col.prop] === 0 ? '':col.unit}}
                </template>
                <template v-else-if="col.label === '使用倍数'">
                  <span>{{scope.row[col.prop]}}</span>
                </template>
                <template v-else-if="col.label === '小计'">
                  {{calculate(col.prop,cel.label.id,scope.$index)}}
                  {{calculate(col.prop,cel.label.id,scope.$index) === '/' ? '':col.unit}}
                </template>
                <template v-else>
                  {{scope.row[col.prop]}}{{col.unit}}
                </template>
              </template>

            </el-table-column>

          </template>
        </el-table-column>

      </el-table>
      <div class="zong">
        <div class="left">
          总量
        </div>
        <div class="rigth" style="display: flex; justify-content: center;">
          <div v-for="(item,index) in headData.filter(p=>p.children.length>0)" :key="index" class="rigthBox"
               style="flex: 1;text-align: center;">
            {{ combined(item.label.id,item.children[0].unit)}}
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
  import Logo from '@/layout/components/Sidebar/Logo'

  export default {
    props: {
      experiment: {
        type: Array,
        default: function() {
          return []
        }
      },
      checkData: {
        type: Object,
        default: function() {
          return {}
        }
      }
    },
    data() {
      return {
        data: {},
        dataTable: {},
        tableData: [],
        headData: [
          {
            label: { label: '放置条件' },
            prop: 'type',
            children: []
          }
        ]
      }
    },
    //是计算属性，也可以理解为一个方法。
    computed: {
      combined() {
        return function(val, unit) {
          let sun = 0, a, b
          for (let i = 0; i < this.experiment.length; i++) {
            a = `小计-${val}`, b = this.experiment[i]
            sun += b[a]
            sun = Math.ceil(sun)
            if (isNaN(sun)) {
              sun = 0
            }
          }
          return `${sun}${unit}` || 0
        }
      },
      handlSample() {
        return function(val) {
          let str = ''
          for (let i = 0; i < val.length; i++) {
            str += val[i].sample_batch
            if (i !== val.length - 1) {
              str = str + '、'
            }
          }
          return str
        }
      }
    },
    //属性是手动定义的所需监听的值
    watch: {
      checkData: {
        handler: function(newV, oldV) {
          if (newV) {
            this.data = newV
            this.headData = [
              {
                label: { label: '放置条件' },
                prop: 'type',
                children: []
              }
            ]
            this.handlData()
          }
        },
        deep: true,
        immediate: true
      }
    },
    //生命周期 - 创建完成（访问当前this实例）
    created() {
    },
    //生命周期 - 挂载完成（访问DOM元素）
    mounted() {
    },
    //是Vue实例对象上绑定的方法，供当前Vue组件作用域内使用，
    methods: {
      //处理数据
      handlData() {
        let arr = [],
          list = []
        for (let key in this.data) {
          for (let i = 0; i < this.data[key].total.a.length; i++) {
            arr.push(this.data[key].total.a[i].arrList)
          }
        }
        if (arr.length === 0) return
        list = arr.reduce((a, b) => {
          return a.concat(b)
        })
        let data = {}, data1 = {}
        for (let j = 0; j < list.length; j++) {
          if (!data1[list[j].idts]) {
            data1[list[j].idts] = { name: list[j].name, list: [] }
          }
          let index = data1[list[j].idts].list.findIndex(a => a.sample.id == list[j].sample.id)
          if (index === -1) {
            data1[list[j].idts].list.push(list[j])
          }

          let name = `${list[j].typeName}${list[j].name}`
          let obj = {
            name: name,
            idts: list[j].idts,
            list: []
          }
          this.$set(data, name, obj)
        }

        for (let key in data) {
          data[key].list = data1[data[key].idts].list
        }

        this.dataTable = data
        let b = []
        for (let key in data) {
          let a = {
            type: data[key].name
          }
          for (let s = 0; s < data[key].list.length; s++) {
            a[data[key].list[s].sample.id] = data[key].list[s].value
          }
          b.push(a)
        }
        this.handlHead(list, b)
      },

      //生成表头
      handlHead(list, val) {
        let data = {}
        for (let i = 0; i < list.length; i++) {
          data[list[i].sample.id] = list[i].sample
        }
        let aaa = []
        for (let key in data) {
          let a = {
            label: {
              id: data[key].id,
              label: data[key].sample_name
            },
            prop: '',
            children: [
              {
                label: '单倍量',
                prop: data[key].id,
                unit: data[key].packaging_unit
              },
              {
                label: '使用倍数',
                prop: '使用倍数' + '-' + data[key].id,
                unit: data[key].packaging_unit
              },
              {
                label: '小计',
                prop: '小计' + '-' + data[key].id,
                unit: data[key].packaging_unit
              }
            ]
          }
          aaa.push(a)
        }

        this.headData = this.headData.concat(aaa)
        this.handlTbale(val)
      },

      //生成表格数据
      handlTbale(val) {
        let arr = [],
          data = {}
        for (let i = 0; i < this.headData.length; i++) {
          data[this.headData[i].prop] = ''
          data.boole = true
          if (this.headData[i].children.length > 0) {
            for (let j = 0; j < this.headData[i].children.length; j++) {
              data[this.headData[i].children[j].prop] = 0
            }
          }
        }
        for (let d = 0; d < val.length; d++) {
          arr.push(data)
          arr[d].type = val[d].name
          let obj1 = arr[d],
            obj2 = val[d]
          Object.keys(obj1).forEach(key => {
            obj1[key] = obj2[key] || obj1[key]
          })
        }

        if (this.tableData.length > 0) {
          this.tableData = this.experiment || []
        } else {
          this.tableData = JSON.parse(JSON.stringify(arr))
        }
      },

      calculate(prop, label, index) {
        let a = '使用倍数' + '-' + label,
          b = label
        let sss = (this.experiment[index][a] * this.experiment[index][b])
        if (this.experiment[index][b] === 0) {
          sss = '/'
        } else {
          sss = Math.ceil(sss)
          if (isNaN(sss)) {
            return 0
          }
        }
        return sss
      }
    }
  }
</script>
<style scoped>
  /* @import url(); 引入css类 */
  .title {
    line-height: 50px;
  }

  ::v-deep .el-table__expanded-cell[class*="cell"] {
    padding: 0 !important;
    border: none;
  }

  ::v-deep
  .el-table__expanded-cell[class*="cell"]
  .el-table__row
  td:first-of-type {
    border-bottom: none;
  }

  ::v-deep .el-button--text {
    padding: 0 !important;
  }

  ::v-deep .table1 tr td {
    padding: 0 !important;
    height: 48px !important;
    overflow: visible !important;
    line-height: 48px;
  }

  ::v-deep .table1 tr td .cell {
    height: 30px !important;
    overflow: visible !important;
    line-height: 30px;
  }

  ::v-deep .item_form {
    overflow: hidden;
  }

  .zong {
    width: 100%;
    height: 48px;
    border: 1px solid #ebeef5;
    border-top: none;
    display: flex;
    align-items: center;
    box-sizing: border-box;
  }

  .left {
    width: 200px;
    height: 48px;
    border-right: 1px solid #ebeef5;
    box-sizing: border-box;
    line-height: 48px;
    padding-left: 20px;
    font-size: 12px;
    text-align: center;
  }

  .rigth {
    width: calc(100% - 200px);
    height: 48px;
    font-size: 12px;
    display: flex;
  }

  .rigthBox {
    height: 48px;
    line-height: 48px;
    border-right: 1px solid #ebeef5;
  }

  .rigthBox:last-child {
    border: none;
  }
</style>
